//
//  ViewController.m
//  19-TheHomeOfCar
//
//  Created by Snow on 16/8/9.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "ModelOfSort.h"
#import "ModelOfCar.h"
#import "ModelOfHotCar.h"
#import "ModelOfMainCar.h"
#import "UIImageView+WebCache.h"
#import "MyCollectionViewCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrOfSort;
@property (nonatomic, strong) NSMutableArray<NSString *> *arrOfIndex;
@property (nonatomic, strong) NSMutableArray *arrOfFindCar;
@property (nonatomic, strong) NSMutableArray *arrOfHotBrand;
@property (nonatomic, strong) NSMutableArray *arrOfMainCar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self handleData1];
    [self handleData2];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"findcar"];
}

- (void)handleData
{
    _arrOfIndex = [NSMutableArray array];
    [_arrOfIndex addObject:@"热"];
    [_arrOfIndex addObject:@"主"];
    _arrOfSort = [NSMutableArray array];
    _arrOfFindCar = [NSMutableArray array];
    NSString *pathOfFind = [[NSBundle mainBundle] pathForResource:@"AllCar" ofType:@"json"];
    NSData *dataOfFind = [NSData dataWithContentsOfFile:pathOfFind];
    NSError *error = nil;
    NSDictionary *dicOfFind = [NSJSONSerialization JSONObjectWithData:dataOfFind options:NSJSONReadingMutableContainers error:&error];
    NSDictionary *dic = [dicOfFind objectForKey:@"result"];
    NSArray *arr = [dic objectForKey:@"brandlist"];
    for (NSDictionary *dict in arr) {
        ModelOfSort *modelOfSort = [[ModelOfSort alloc] init];
        [modelOfSort setValuesForKeysWithDictionary:dict];
        [_arrOfSort addObject:modelOfSort];
        [_arrOfIndex addObject:modelOfSort.letter];
    }
    
}
- (void)handleData1
{
    _arrOfHotBrand = [NSMutableArray array];
    NSString *pathOfHot = [[NSBundle mainBundle] pathForResource:@"HotBrand" ofType:@"json"];
    NSData *dataOfHot = [NSData dataWithContentsOfFile:pathOfHot];
    NSError *error = nil;
    NSDictionary *dicOfHot = [NSJSONSerialization JSONObjectWithData:dataOfHot options:NSJSONReadingMutableContainers error:&error];
    NSDictionary *dic = [dicOfHot objectForKey:@"result"];
    NSArray *arr = [dic objectForKey:@"list"];
    for (NSDictionary *dictionary in arr) {
        ModelOfHotCar *model = [[ModelOfHotCar alloc] init];
        [model setValuesForKeysWithDictionary:dictionary];
        [_arrOfHotBrand addObject:model];
    }
}
- (void)handleData2
{
    _arrOfMainCar = [NSMutableArray array];
    NSString *pathOfMain = [[NSBundle mainBundle] pathForResource:@"Extension" ofType:@"json"];
    NSData *dataOfMain = [NSData dataWithContentsOfFile:pathOfMain];
    NSError *error = nil;
    NSDictionary *dicOfMain = [NSJSONSerialization JSONObjectWithData:dataOfMain options:NSJSONReadingMutableContainers error:&error];
    NSDictionary *dic = [dicOfMain objectForKey:@"result"];
    NSArray *arr = [dic objectForKey:@"list"];
    for (NSDictionary *dictionary in arr) {
        ModelOfMainCar *model = [[ModelOfMainCar alloc] init];
        [model setValuesForKeysWithDictionary:dictionary];
        [_arrOfMainCar addObject:model];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _arrOfSort.count + 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else if(section == 1) {
        return 0;
    } else {
        ModelOfSort *model = [_arrOfSort objectAtIndex:section - 2];
        NSArray *array = model.list;
        return array.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return nil;
    } else if (indexPath.section == 1) {
        return nil;
    } else {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"findcar"];
        ModelOfSort *model = [_arrOfSort objectAtIndex:indexPath.section - 2];
        NSArray *array = model.list;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dictionary in array) {
        ModelOfCar *modelOfCar = [[ModelOfCar alloc] init];
        [modelOfCar setValuesForKeysWithDictionary:dictionary];
        [arr addObject:modelOfCar];
        }
    ModelOfCar *modelOfcar = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = modelOfcar.name;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:modelOfcar.imgurl]];
    return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    } else if (section == 1) {
    return nil;
    }
    return [_arrOfIndex objectAtIndex:section];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *viewOfHot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 10, 250)];
        viewOfHot.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, viewOfHot.frame.size.width, 20)];
        [viewOfHot addSubview:label];
        label.text = @"热门品牌";
        label.font = [UIFont systemFontOfSize:12];
        UIView *viewOfLine = [[UIView alloc] initWithFrame:CGRectMake(0, 35, viewOfHot.frame.size.width, 1)];
        viewOfLine.backgroundColor = [UIColor lightGrayColor];
        [viewOfHot addSubview:viewOfLine];
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.itemSize = CGSizeMake(viewOfHot.frame.size.width / 5, 100);
        flow.minimumLineSpacing = 0;
        flow.minimumInteritemSpacing = 0;
        flow.sectionInset = UIEdgeInsetsZero;
        UICollectionView *collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, viewOfHot.frame.size.width, 210) collectionViewLayout:flow];
        collectionview.delegate = self;
        collectionview.dataSource = self;
        collectionview.backgroundColor = [UIColor clearColor];
        [viewOfHot addSubview:collectionview];
        [collectionview registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"collectionView"];
        return viewOfHot;
    } else if (section == 1) {
        
        UIView *viewOfMain = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 10, 150)];
        viewOfMain.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, viewOfMain.frame.size.width, 20)];
        [viewOfMain addSubview:label];
        label.text = @"主打车";
        label.font = [UIFont systemFontOfSize:12];
        UIView *viewOfLine = [[UIView alloc] initWithFrame:CGRectMake(0, 35, viewOfMain.frame.size.width, 1)];
        viewOfLine.backgroundColor = [UIColor lightGrayColor];
        [viewOfMain addSubview:viewOfLine];
        for (int i = 0; i < _arrOfMainCar.count; i++) {
            ModelOfMainCar *model = [_arrOfMainCar objectAtIndex:i];
            UIImageView *image = [[UIImageView alloc] init];
            image.frame = CGRectMake(viewOfMain.frame.size.width / _arrOfMainCar.count * i, 40, viewOfMain.frame.size.width / _arrOfMainCar.count, 80);
            [viewOfMain addSubview:image];
            [image sd_setImageWithURL:[NSURL URLWithString:model.img]];
            UILabel *labelOfTitle = [[UILabel alloc] initWithFrame: CGRectMake(viewOfMain.frame.size.width / _arrOfMainCar.count * i, 40 + 80 + 10, viewOfMain.frame.size.width / _arrOfMainCar.count, 20)];
            labelOfTitle.text = model.seriesname;
            labelOfTitle.font = [UIFont systemFontOfSize:12];
            labelOfTitle.textAlignment = 1;
            [viewOfMain addSubview:labelOfTitle];
        }
        return viewOfMain;
    } else {
        return nil;
    }
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrOfHotBrand.count;
}
- (UICollectionViewCell *)
collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView" forIndexPath:indexPath];
    ModelOfHotCar *model = [_arrOfHotBrand objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.img]];
    cell.label.text = model.name;
    cell.label.font = [UIFont systemFontOfSize:12];
    cell.label.textAlignment = 1;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 250;
    } else if (section == 1) {
        return 150;
    } else {
    return 10;
    }
}
//快速索引
// 索引目录
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return _arrOfIndex;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
